﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Ycm20250401.Service._20250418
{
    public class UserService
    {
        private readonly IUserRepository _userRepository;
        private readonly IValidatePassword _passwordHasher;

        public UserService(IUserRepository userRepository, IValidatePassword passwordHasher)
        {
            _userRepository = userRepository;
            _passwordHasher = passwordHasher;
        }
        public UserService()
        {
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<bool> GetLogoin(string username, string password)
        {
            var user =await _userRepository.GetUserByUserNameAsync(username);
            if (user == null)
            {
                return false;
            }
            var loginResult=await user.PasswordIsValid(_passwordHasher,password);
            return loginResult;
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task<bool> Register(string username, string password)
        {
            var user = await _userRepository.GetUserByUserNameAsync(username);
            if (user != null)
            {
                return false;
            }
            var addUser =await UserRegister.AddUser(username, "", "", password, _passwordHasher);
            await _userRepository.AddUserAsync(addUser);
            return true;
        }
    }
}
